/**
 * 
 */
var thispage = {
		init: function(){
			this.tableInit();
			this.initObj();
			this.initEvents();
		},
		initObj: function(){
			
		},
		initEvents: function(){
			var t = this;
			$('.d-tbl .layui-btn').on('click', function() {
				var type = $(this).attr('@event');
				t.funs[type] ? t.funs[type].call(this) : '';
			});
		},
		funs: {
			add: function(){
				pub.openEdit("新增用户", ctx + "/user/edit", [ '420px', '250px' ]);
			},
			//删除行
			editRow: function(rowId){
				pub.openEdit("修改用户", ctx + "/user/edit?id=" + rowId, [ '420px', '250px' ]);
			},
			//编辑行
			delRow: function(rowId){
				layer.confirm('确定要删除用户吗', function(index) {
					layer.close(index);
					//向服务端发送删除指令
					$.post(ctx +"/user/delete", {id: rowId}, function(ret){
						if(ret.state == "ok"){
							thispage.funs.search();
						}else{
							layer.alert(ret.msg || "服务器异常");
						}
					})
				});
			},
			//角色授权
			editUserRole: function(){
				var selRow = $('#tblmain').bootstrapTable('getSelections')[0];
				if(selRow){
					pub.openEdit("用户授权(" + selRow.name + ")", ctx + "/user/editUserRole?userId="+ selRow.id, [ '820px', '450px' ]);
				}else{
					layer.msg('请选择要授权的用户。', {icon: 2});
				}
			},
			search:  function(where){
				//执行重载
				$('#tblmain').bootstrapTable('refresh', {query: where || {}});
			}
		},
		tableInit: function(){
			var t = this;
			var maxH = document.documentElement.clientHeight;
			var height = maxH;
			var columns = [{
				checkbox: true
			},{
				field: 'id',
				title: 'ID',
				visible: false
			},{
				field: 'account',
				title: '用户账号',
				edit:{type:'text' },
				sortable: true
			}, {
				field: 'name',
				title: '用户名称',
				sortable: true
			}, {
				field: 'enable',
				title: '是否启用',
				align: "center",
				formatter: tableDefault.cellChekboxFmt,
				sortable: true
			}, {
				field: 'createtime',
				title: '创建时间',
				sortable: true
			}, {
				field: "option",
				title: "操作",
				align: "center",
				formatter: function(value, row, rowIndex){
					return "<span class='option glyphicon glyphicon-edit' aria-hidden='true' onclick=thispage.funs.editRow('"+row.id+"') ></span>" +
						   "<span class='option glyphicon glyphicon-remove' aria-hidden='true' onclick=thispage.funs.delRow('"+row.id+"') ></span>";
				}
			}];
			var tableParams = $.extend(tableDefault.params, {
				url: ctx + '/user/query',
				sortOrder: "asc",   //排序方式
				sortName: "createtime", //默认排序字段
				pageSize: 15,   //每页的记录行数（*）
				singleSelect: true, //禁止多选
				columns: columns,
				height: height,   //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
				onDblClickRow: function(row, $elemen, field){
					thispage.funs.editRow(row.id);
				}
			});
			$('#tblmain').bootstrapTable(tableParams);
		}
}



layui.use([ 'layer' ], function() {
	var table = layui.table;
	var layer = layui.layer;
	thispage.init();
	//提供给editMenu 调用
	window.events = {
			search: function(){
				thispage.funs.search();
			}
	}
});
